from rest_framework import serializers
from users.models import User


class UserSerializer(serializers.ModelSerializer):
    password = serializers.CharField()

    class Meta:
        model = User
        fields = ("id", "username", "mobile", "email", "password")

        extra_kwargs = {
            "password": {
                "write_only": True,
                "max_length": 20,
                "min_length": 8
            },
            "username":{
                'max_length': 20,
                'min_length': 5
            }
        }

    def create(self, validated_data):
        user = super().create(validated_data)
        user.set_password(user.password)
        user.save()
        return user
